package com.xatz.scanner

import android.app.Activity
import android.content.Context
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.Toast
import androidx.core.content.ContextCompat

/**
 * author：tdq
 * date：2021/6/10
 * description：
 */

fun Context.toast(message: CharSequence, duration: Int = Toast.LENGTH_LONG) {
    val toast = Toast.makeText(this, message, duration)
    toast.setGravity(Gravity.CENTER, 0, 0)
    val view = toast.view
    view?.setBackgroundResource(R.color.color_ff5555)
    toast.view = view
    toast.show()
}

val Context.inputMethodManager
    get() = ContextCompat.getSystemService(this, InputMethodManager::class.java)

fun Activity.hideKeyboard() {
    inputMethodManager?.hideSoftInputFromWindow((currentFocus ?: View(this)).windowToken, 0)
    window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN)
    currentFocus?.clearFocus()
}

fun Activity.showKeyboard(et: EditText) {
    et.requestFocus()
    inputMethodManager?.showSoftInput(et, InputMethodManager.SHOW_IMPLICIT)
}